<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="所参与项目" prop="projectIds">
        <el-select v-model="queryParams.projectIds" placeholder="请选择项目">
          <el-option
            v-for="item in projectOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入艺人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签" prop="labs">
        <el-select v-model="queryParams.labs" multiple placeholder="选择标签">
          <el-option
            v-for="item in labsOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input
          v-model="queryParams.sex"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="籍贯" prop="hometown">
        <el-input
          v-model="queryParams.hometown"
          placeholder="请输入籍贯"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经纪公司" prop="agent">
        <el-input
          v-model="queryParams.agent"
          placeholder="请输入经纪公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-hasRole="['@systemAdmin', '@employee']">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5" v-hasRole="['@systemAdmin']">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="artistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="标签" align="center" min-width="200">
        <template slot-scope="scope">
          <el-tag
            style="display: inline-block; margin-bottom: 4px; margin-right: 4px"
            v-for="tag in scope.row.labNames"
            :key="tag"
          >
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="籍贯" align="center" prop="hometown" />
      <el-table-column label="昵称/外号" align="center" prop="nickName" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="有/无经纪公司" align="center" prop="agent" min-width="200" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="190"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="showDetail(scope.row)"
            v-hasPermi="['system:artist:edit']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:artist:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            style="color: #f56c6c"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasRole="['@systemAdmin']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <edit ref="editForm" :projectOptions="projectOptions" @update="getList" />
    <detail ref="detailForm" />
  </div>
</template>

<script>
import { listArtist, getArtist, delArtist } from '@/api/system/artist'
import { listProject } from '@/api/system/project'
import { listLab } from '@/api/system/lab'
import edit from './businessComponents/edit.vue'
import detail from './businessComponents/detail.vue'

export default {
  name: 'Artist',
  components: { edit, detail },

  data() {
    return {
      labsOption: [],
      projectOptions: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 艺人管理表格数据
      artistList: [],
      labs: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        projectIds: null,
        name: null
      }
    }
  },
  created() {
    this.getLabsList()

    this.getList()
    this.getProjectList()
  },
  methods: {
    getLabsList() {
      listLab({
        pageNum: 1,
        pageSize: 999
      }).then(response => {
        this.labsOption = response.rows.map(item => {
          return {
            label: item.name,
            value: item.id
          }
        })
      })
    },
    /** 查询艺人管理列表 */
    getList() {
      this.loading = true
      listArtist(this.queryParams)
        .then(response => {
          this.artistList = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    getProjectList() {
      listProject({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.projectOptions = res.rows.map(item => {
          return {
            label: item.name,
            value: item.id
          }
        })
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')

      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.editForm.open('添加艺人管理', {})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getArtist(id).then(response => {
        this.$refs.editForm.open('修改艺人管理', { ...response.data, isEditArt: true })
      })
    },
    // 展示艺人详情
    showDetail(formData) {
      this.$refs.detailForm.open('艺人详情', formData)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      const names = row.name || this.names
      this.$modal
        .confirm('是否确认删除艺人名称为"' + names + '"的数据项？')
        .then(function () {
          return delArtist(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>
